//
//  OosMeLoader.m
//  oos4ios
//
//  Created by Pablo Gonzalez on 4/13/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import "OosMeLoader.h"
#import <oos4ios/Oos4ios.h>
#import <oos4ios/DAOs/OosUserDao.h>
#import <oos4ios/Model/OosObject.h>

// Identifiers for tagging the ticket returned by the Dao
enum {
    initialIdentifier,
    servicesIdentifier,
    contactsIdentifier,
    tagsIdentifier,
    listsIdentifier,
    reviewIdentifier,
    finishedIdentifier
};

@implementation OosMeLoader

@synthesize callback = mCallback;
@synthesize active = mActive;

static OosMeLoader *loader;

- (id)init
{
    self = [super init];
    if (self) {
        self.active = YES;
    }
    return self;
}

static OosDao *dao = nil;

- (void) start: (int) identifier
{
    if (self.active) { // only continue if the Loader is active
        int newIdentifier = -1;
        switch (identifier) {
            case initialIdentifier:
                [dao release];
                dao = [[OosUserDao daoFromMe] retain];
                newIdentifier = finishedIdentifier;
                break;
            case finishedIdentifier:
                if (self.callback) {
                    [self.callback loadedWith: [Oos4ios instance].me];
//                    self.callback = nil;
                }
                return;
            default:
                break;
        }
        if (newIdentifier > -1) {
            [dao read: self].identifier = newIdentifier;
        } else {
            NSLog(@"Bam!");
        }
    }
}

- (void) stop 
{
    // Set the flag to stop load on next sequence.
    self.active = NO;
}

+ (void)startLoadingMe:(id)callback
{
    if (loader) {
        // If there is a loader running, stop it, release and create a new one
        [loader stop];
        [loader release];
        loader = nil;
    }
    loader = [[OosMeLoader alloc] init];
    loader.callback = callback;
    // Start with the first sequence.
    [loader start: initialIdentifier];
}

- (void) readResult: (NSArray *) array
		 withTicket: (OosDaoTicket *) ticket
{
    id first = nil;
    if ([array count] > 0) {
        first = [array objectAtIndex:0];
    }
    if(mMe && first) {
        if ([first isKindOfClass:[OosTag class]]) {
            for (OosTag *tag in array) {
                [mMe.mutableTags addObject:tag];
            }
        } else if([first isKindOfClass: [OosList class]]) {
            for (OosList *list in array) {
                [mMe.mutableLists addObject:list];
            }
        }
    }
    
    if (first && [first isKindOfClass: [OosMe class]]) {
        mMe = [array objectAtIndex:0];
        [self start: ticket.identifier];
    } else if ([array count] == 200) {
        int page = [dao.options.pageOptions.page intValue] + 1;
        dao.options.pageOptions.page = [NSNumber numberWithInt:page];
        [dao read: self].identifier = ticket.identifier;
    } else {
        [self start: ticket.identifier];
    }
}

- (void) resultedInError: (NSError *) error 
			  withTicket: (OosDaoTicket *) ticket
{
    if (self.callback) {
        [self.callback failedWith: error];
    }
}

- (void)setMe:(OosMe *)me
{
    if ([NSThread isMainThread]) {
        [me retain];
        [mMe release];
        mMe = me;
    } else {
        mMeId = [me objectID];
    }
}

- (OosMe *)me
{
    if (!mMe && mMeId) {
        mMe = (OosMe *) [[[Oos4ios instance] managedContext] objectWithID:mMeId];
        [mMe retain];
    }
    return mMe;
}

- (void)dealloc
{
    [mCallback release];
    [mMe release];
    [super dealloc];
}

@end
